﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Configuration;
using System.Data.SqlClient;
using System.IO;
namespace NCKH.Admin
{
    public partial class tintuc : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            txttimkiem.Attributes.Add("PlaceHolder", "Nhập tiêu đề tin cần tìm...");
            if (!IsPostBack)
            {
                hientendanhmuctin();
            }

            hientintuc();
        }

        protected void hientendanhmuctin()
        {
            string connectionstring = ConfigurationManager.ConnectionStrings["NCKH"].ConnectionString;
            string sqlselect = "SELECT Madanhmuctin, Tendanhmuctin FROM DANHMUCTIN";
            using (SqlConnection cnn = new SqlConnection(connectionstring))
            {
                using (SqlCommand cmd = new SqlCommand(sqlselect, cnn))
                {
                    cnn.Open();
                    cmd.CommandType = CommandType.Text;
                    using (SqlDataReader rd = cmd.ExecuteReader())
                    {
                        if (rd.HasRows)
                            while (rd.Read())
                            {
                                ddldanhmuctin.Items.Add(
                                    new ListItem(rd["Tendanhmuctin"].ToString(), rd["Madanhmuctin"].ToString()));
                            }
                        else
                            btntimkiem.Enabled = false;
                        rd.Close();
                    }
                    cnn.Close();
                }
            }
        }

        protected void hientintuc()
        {
            using (DataTable tb = gettintuc())
            {
                grvtintuc.DataSource = tb;
                grvtintuc.DataBind();

            }
        }
        protected DataTable gettintuc()
        {
            string connectionstring = ConfigurationManager.ConnectionStrings["NCKH"].ConnectionString;
            string select_tin = "SELECT Manoidungtin, Tieude, Tendanhmuctin, Tomtat, Ngaytao, Noidung, Tag, Image, bhienthi, trangthaitin FROM NOIDUNGTIN, DANHMUCTIN WHERE NOIDUNGTIN.Madanhmuctin = DANHMUCTIN.Madanhmuctin";
            using (SqlConnection cnn = new SqlConnection(connectionstring))
            {
                using (SqlCommand cmd = new SqlCommand(select_tin, cnn))
                {
                    cmd.CommandType = CommandType.Text;
                    using (SqlDataAdapter da = new SqlDataAdapter(cmd))
                    {
                        DataTable tbl = new DataTable();
                        da.Fill(tbl);
                        return tbl;

                    }
                }
            }
        }

        protected void btntimkiem_Click(object sender, EventArgs e)
        {
            int catID = Convert.ToInt32(ddldanhmuctin.SelectedValue);
            using (DataTable tb = hientimkiemtintuc(catID))
            {
                grvtintuc.DataSource = tb;
                grvtintuc.DataBind();
            }
        }

        protected DataTable hientimkiemtintuc(int catID)
        {
            string connectionString = ConfigurationManager.ConnectionStrings["NCKH"].ConnectionString;
            using (SqlConnection cnn = new SqlConnection(connectionString))
            {
                using (SqlCommand cmd = new SqlCommand("timkiemtintuc", cnn))
                {
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.Parameters.AddWithValue("@Tieude", '%' + txttimkiem.Text + '%');
                    cmd.Parameters.AddWithValue("@Madanhmuctin", catID);
                    using (SqlDataAdapter da = new SqlDataAdapter(cmd))
                    {
                        da.SelectCommand = cmd;
                        DataTable tbl = new DataTable();
                        da.Fill(tbl);
                        return tbl;
                    }
                }
            }
        }


        protected void grvtintuc_RowDataBound(object sender, GridViewRowEventArgs e)
        {

        }

        protected void lbthienthi_Command(object sender, CommandEventArgs e)
        {

        }

        protected void btnthem_Click(object sender, EventArgs e)
        {
            Response.Redirect("themtintuc.aspx");
        }

        protected void grvtintuc_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            grvtintuc.PageIndex = e.NewPageIndex;
            hientintuc();
        }

        protected void txttimkiem_TextChanged(object sender, EventArgs e)
        {


        }

        protected void grvtintuc_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        protected void grvtintuc_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            int matinID = Convert.ToInt32(e.CommandArgument);
            if (e.CommandName.ToLower().Equals("xoa"))
            {

                string connectionstring = ConfigurationManager.ConnectionStrings["NCKH"].ConnectionString;
                string delete_tin = "DELETE FROM NOIDUNGTIN WHERE Manoidungtin='" + matinID + "'";
                using (SqlConnection cnn = new SqlConnection(connectionstring))
                {
                    using (SqlCommand cmd = new SqlCommand(delete_tin, cnn))
                    {
                        cmd.CommandType = CommandType.Text;
                        cnn.Open();
                        cmd.ExecuteNonQuery();
                        cnn.Close();
                        hientintuc();
                    }
                }
            }

        }

    }
}